import { useEffect, useRef } from 'react'
import THREEProject from '../utils/index'

const MaterialDemo = () => {
  const can = useRef(null)
  const pro = new THREEProject()
  const { thr, renderer, scene, axes, camera, controls } = pro

  const init = () => {
    scene.add(axes)
    const textureLoader = new thr.TextureLoader()
    const light = new thr.AmbientLight(0xffffff)
    const geometry = new thr.BoxBufferGeometry(5,5,5)
    const material = new thr.MeshLambertMaterial({
      color: 0x331133,
      opacity: 0.8,
      transparent: true
    })
    const mesh = new thr.Mesh(geometry, material)
    scene.add(light)
    scene.add(mesh)
    appendRenderDom()
    animate()
  }
  const appendRenderDom = () => {
    if(!can.current.children.length) {
      can.current.appendChild(renderer.domElement)
    }
  }

  const render = () => {
    controls.update()
    renderer.render(scene, camera)
  }

  const animate = () => {
    requestAnimationFrame(animate)
    render()
  }

  useEffect(init, [])

  return (
    <div ref={can}></div>
  )
}

export default MaterialDemo